package sh.wn36.vehiclemanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import org.thymeleaf.util.StringUtils;
import sh.wn36.vehiclemanage.entity.AccidentRecord;
import sh.wn36.vehiclemanage.entity.dto.AccidentQuery;
import sh.wn36.vehiclemanage.service.AccidentRecordService;
import sh.wn36.vehiclemanage.util.Result;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;


/**
 * 事故记录
 * @author lzp
 */
@RestController
@RequestMapping("/accident-record")
@Api("事故记录")
@CrossOrigin
public class AccidentRecordController {

    AccidentRecordService accidentRecordService;

    @Autowired
    public void setAccidentRecordService(AccidentRecordService accidentRecordService) {
        this.accidentRecordService = accidentRecordService;
    }
    @ApiOperation("查询单个事故记录，回调给修改数据的对象")
    @GetMapping("/selectAccidentById/{id}")
    public Result selectAccidentById(@ApiParam @PathVariable Integer id) {
        AccidentRecord record = accidentRecordService.getById(id);
        return  Result.OK().data("items",record);
    }
    @ApiOperation("事故记录登记")
    @PostMapping("/addOne")
    public Result addAccidentRecord(@RequestBody AccidentRecord accidentRecord) {
        boolean b = accidentRecordService.save(accidentRecord);
        if (b) {
            return Result.OK();
        } else {
            return Result.Fail();
        }
    }

    @ApiOperation("删除事故记录")
    @PostMapping("/deleteAccidentByIds/{ids}")
    public Result deleteAccidentByIds(@ApiParam(value = "ids", name = "ids") @PathVariable List<Integer> ids) {
        boolean b = accidentRecordService.removeByIds(ids);
        if (b) {
            return Result.OK();
        } else {
            return Result.Fail();
        }
    }

    @ApiOperation("修改记录")
    @PostMapping("/update")
    public Result update(@RequestBody AccidentRecord newRecord) {
        boolean b = accidentRecordService.updateById(newRecord);
        if (b) {
            return Result.OK();
        } else {
            return Result.Fail();
        }
    }

    @ApiOperation("报表接口,查询所有")
    @GetMapping("/reportList")
    public Result reportList() {
        List<Map<String, Object>> maps = accidentRecordService.reportList();
        return Result.OK().data("items", maps);
    }
    @ApiOperation("报表接口,根据车牌号查询")
    @GetMapping("/queryReportByCarId/{carId}")
    public Result queryReportByCarId(@ApiParam(value = "车的id",name = "carId") @PathVariable Integer carId) {
        List<Map<String, Object>> maps = accidentRecordService.queryReportByCarId(carId);
        return Result.OK().data("items", maps);
    }

    @ApiOperation("查询所有的事故记录")
    @PostMapping("/conditionSelectMapList/{nowPage}/{pageSize}")
    public Result conditionSelectMapList(
            @ApiParam(value = "当前页码", name = "nowPage", required = true) @PathVariable Integer nowPage,
            @ApiParam(value = "每页数据量", name = "pageSize", required = true) @PathVariable Integer pageSize,
            @ApiParam(value = "条件", name = "conditionQuery") @RequestBody(required = false) AccidentQuery accidentQuery
    ) {
        Page<Map<String, Object>> accidentRecord = new Page<>(nowPage, pageSize);
        //-- 构建查询条件对象
        QueryWrapper<AccidentQuery> wrapper = new QueryWrapper<>();

        //判断条件是否为空，不为空则加入查询条件
        String carNo = accidentQuery.getCarNo();
        String driverName = accidentQuery.getDriverName();
        if (!StringUtils.isEmpty(carNo)) {
            wrapper.like("car.car_no", carNo);
        }
        if (!StringUtils.isEmpty(driverName)) {
            wrapper.like("driver.name", driverName);
        }
        List<Map<String, Object>> accidentList = accidentRecordService.selectAccidentRecordMap(accidentRecord, wrapper);

//        时间戳转换
        for (Map<String, Object> map : accidentList) {
            Timestamp timestamp = (Timestamp) map.get("acc_date");
            if(timestamp!=null){
                Date dateTime = new Date(timestamp.getTime());
                map.replace("acc_date", dateTime);
            }
        }

        long total = accidentRecord.getTotal();
        return Result.OK().data("items", accidentList).data("total", total);
    }

}

